#include "head.h"

int whichOrder(char* orderList,int* orderflag,char* args)
{
    char* order = strtok(orderList," ");
    char* tmp = strtok(NULL," ");
    if(tmp != NULL)
    {
        memcpy(args,tmp,strlen(tmp));
    }
    char buf[4];
    strncpy(buf,orderList,3);
    buf[3] = '\0';

    if(strcmp(order,"cd") == 0)
    {
        *orderflag = CD;
        //printf("cd\n");
    }
    else if(strcmp(order,"ls") == 0)
    {
        *orderflag = LS;
        //printf("ls\n");
    }
    else if(strcmp(order,"puts") == 0)
    {
        *orderflag = PUTS;
        //printf("puts\n");
    }
    else if(strcmp(order,"gets") == 0)
    {
        *orderflag = GETS;
        //printf("gets\n");
    }
    else if(strcmp(order,"remove") == 0)
    {
        *orderflag = REMOVE;
        //printf("remove\n");
    }
    else if(strcmp(buf,"pwd") == 0)
    {
        *orderflag = PWD;
        //printf("pwd\n");
    }
    else if(strcmp(order,"mkdir") == 0)
    {
        *orderflag = MKDIR;
        //printf("mkdir\n");
    }
    else if(strcmp(order,"rmdir") == 0)
    {
        *orderflag = RMDIR;
        //printf("rmdir\n");
    }
    else
    {
        printf("Illegal Order!\n");
        return -1;
    }

    return 0;
}

int transOrder(int netfd,int orderflag,char* args)
{
    train_t train;
    bzero(&train,sizeof(train));
    train.length = sizeof(int) + strlen(args);
    sprintf(train.data,"%d%s",orderflag,args);
    send(netfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    return 0;
}

int Login(int sockfd)
{
    printf("Please enter your username: "); 
    fflush(stdout);
    char username[256] = {0};
    scanf("%s",username);

    printf("Please enter your userpasswd: "); 
    fflush(stdout);
    char userpasswd[256] = {0};
    scanf("%s",userpasswd);

    train_t train;
    train.length = strlen(username);
    strcpy(train.data,username);
    send(sockfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    bzero(&train,sizeof(train));
    train.length = strlen(userpasswd);
    strcpy(train.data,userpasswd);
    send(sockfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    char loginFlag;
    recv(sockfd,&loginFlag,sizeof(char),MSG_WAITALL);
    if(loginFlag == '1')
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

int usrRegister(int sockfd)
{
    printf("Please enter your username: "); 
    fflush(stdout);
    char username[256] = {0};
    scanf("%s",username);

    printf("Please enter your userpasswd: "); 
    fflush(stdout);
    char userpasswd[256] = {0};
    scanf("%s",userpasswd);

    train_t train;
    train.length = strlen(username);
    strcpy(train.data,username);
    send(sockfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    bzero(&train,sizeof(train));
    train.length = strlen(userpasswd);
    strcpy(train.data,userpasswd);
    send(sockfd,&train,sizeof(int) + train.length,MSG_NOSIGNAL);

    char dupli;
    recv(sockfd,&dupli,1,MSG_WAITALL);
    if(dupli == '1')
    {
        printf("Duplicate Username!\n");
        return -1;
    }

    return 0;
}

int recvLs(int netfd)
{
    char buff[1024]={0};
    int length;
    while(1)
    {
        memset(buff,0,sizeof(buff));
        int ret=recv(netfd,&length,sizeof(int),MSG_WAITALL);
        if(length==0)
        {
            break;
        }
        recv(netfd,buff,length,MSG_WAITALL);
        printf("%s\n",buff);
    }
    return 0;

}

int recvPwd(int netfd)
{
    char buff[1024]={0};
    int length;
    recv(netfd,&length,sizeof(int),MSG_WAITALL);
    recv(netfd,buff,length,MSG_WAITALL);
    printf("%s\n",buff);
    return 0;
}
